package com.m3958.firstgwt.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.inject.Inject;
import com.m3958.firstgwt.client.datasource.AssetFolderDataSource;
import com.m3958.firstgwt.client.datasource.SectionDataSource;
import com.m3958.firstgwt.client.datasource.XinJianCatDataSource;
import com.m3958.firstgwt.client.layout.Iview;
import com.m3958.firstgwt.client.place.ArticleEditPlace;
import com.m3958.firstgwt.client.place.ArticleListPlace;
import com.m3958.firstgwt.client.place.AssetEditPlace;
import com.m3958.firstgwt.client.place.AssetFolderEditPlace;
import com.m3958.firstgwt.client.place.AssetFolderTreePlace;
import com.m3958.firstgwt.client.place.AssetListPlace;
import com.m3958.firstgwt.client.place.AsyncTaskListPlace;
import com.m3958.firstgwt.client.place.CommentEditPlace;
import com.m3958.firstgwt.client.place.CommentTreePlace;
import com.m3958.firstgwt.client.place.DataTemplateEditPlace;
import com.m3958.firstgwt.client.place.DataTemplateListPlace;
import com.m3958.firstgwt.client.place.DiskFileEditPlace;
import com.m3958.firstgwt.client.place.DiskFileListPlace;
import com.m3958.firstgwt.client.place.FieldEnumEditPlace;
import com.m3958.firstgwt.client.place.FieldEnumPlace;
import com.m3958.firstgwt.client.place.FtlEditPlace;
import com.m3958.firstgwt.client.place.FtlListPlace;
import com.m3958.firstgwt.client.place.GroupEditPlace;
import com.m3958.firstgwt.client.place.GroupTreePlace;
import com.m3958.firstgwt.client.place.HmessagePlace;
import com.m3958.firstgwt.client.place.LinkEditPlace;
import com.m3958.firstgwt.client.place.LinkTreePlace;
import com.m3958.firstgwt.client.place.MenuItemEditPlace;
import com.m3958.firstgwt.client.place.MenuItemPlace;
import com.m3958.firstgwt.client.place.MenuLevelEditPlace;
import com.m3958.firstgwt.client.place.MenuLevelListPlace;
import com.m3958.firstgwt.client.place.ObjectClassEditPlace;
import com.m3958.firstgwt.client.place.ObjectClassListPlace;
import com.m3958.firstgwt.client.place.PageMistakeEditPlace;
import com.m3958.firstgwt.client.place.PageMistakeListPlace;
import com.m3958.firstgwt.client.place.RoleEditPlace;
import com.m3958.firstgwt.client.place.RolePlace;
import com.m3958.firstgwt.client.place.SectionEditPlace;
import com.m3958.firstgwt.client.place.SectionTreePlace;
import com.m3958.firstgwt.client.place.SiteConfigEditPlace;
import com.m3958.firstgwt.client.place.SiteConfigListPlace;
import com.m3958.firstgwt.client.place.TableSchemaEditPlace;
import com.m3958.firstgwt.client.place.TableSchemaListPlace;
import com.m3958.firstgwt.client.place.UrlStatisticsListPlace;
import com.m3958.firstgwt.client.place.UserEditPlace;
import com.m3958.firstgwt.client.place.UserListPlace;
import com.m3958.firstgwt.client.place.VoteEditPlace;
import com.m3958.firstgwt.client.place.VoteTplEditPlace;
import com.m3958.firstgwt.client.place.VoteTplListPlace;
import com.m3958.firstgwt.client.place.VoteTreePlace;
import com.m3958.firstgwt.client.place.WebHostEditPlace;
import com.m3958.firstgwt.client.place.WebHostListPlace;
import com.m3958.firstgwt.client.place.WebSiteEditPlace;
import com.m3958.firstgwt.client.place.WebSitePlace;
import com.m3958.firstgwt.client.place.WelcomePlace;
import com.m3958.firstgwt.client.place.XinJianCatEditPlace;
import com.m3958.firstgwt.client.place.XinJianCatTreePlace;
import com.m3958.firstgwt.client.place.XinJianListPlace;
import com.m3958.firstgwt.client.place.XinjianEditPlace;
import com.m3958.firstgwt.client.types.ViewNameEnum;
import com.m3958.gwtutil.clientside.client.YuiWrapper;
import com.m3958.gwtutil.clientside.client.utils.ClientStringUtils;

public class DataPanelManager implements ValueChangeHandler<String>{

	@Inject
	private YuiWrapper yui;
	
	@Inject
	private ClientStringUtils csu;
	
	@Inject
	private DataPanel dp;
	
	@Inject
	private LayoutService vs;
	
	public void fillLayouts(){
		for(Iview iv : vs.getAllViews()){
			if(iv != null && iv.asLayout() != null){
				dp.addMember(iv.asLayout());
				dp.hideMember(iv.asLayout());
			}
		}
	}
	
	private ViewNameEnum getViewNameEnum(String token){
		ViewNameEnum vn;
		try {
			vn = ViewNameEnum.valueOf(ViewNameEnum.class, token.toUpperCase());
		} catch (Exception e) {
			yui.logString(token);
			vn = ViewNameEnum.WELCOME;
		}
		return vn;
	}
	
	private MyPlace token2Place(String token){
		if(csu.isTokenEmpty(token))return new WelcomePlace();
		String[] ss = token.split("/",2);
		
		if(ss.length < 1)return null;
		String vn = ss[0];
		ViewNameEnum vne = getViewNameEnum(vn);
		
		String p1 = null;
		if(ss.length > 1)p1 = ss[1];
		if(csu.isTokenEmpty(p1))p1 = "";
		switch (vne) {
		case ARTICLE:
			return new ArticleListPlace(p1);
		case ASSET:
			return new AssetListPlace(p1);
		case ASSET_EDIT:
			return new AssetEditPlace(p1);
		case ASSET_FOLDER:
			return new AssetFolderTreePlace(p1);
		case ASSET_FOLDER_EDIT:
			return new AssetFolderEditPlace(p1);
		case FIELD_ENUM:
			return new FieldEnumPlace();
		case FIELD_ENUM_EIDT:
			return new FieldEnumEditPlace(p1);
		case MENUITEM:
			return new MenuItemPlace(p1);
		case MENUITEM_EDIT:
			return new MenuItemEditPlace(p1);
		case WEBSITE:
			return new WebSitePlace();
		case WEBSITE_EDIT:
			return new WebSiteEditPlace(p1);
		case SECTION:
			return new SectionTreePlace(p1);
		case SECTION_EDIT:
			return new SectionEditPlace(p1);
		case ARTICLE_EDIT:
			return new ArticleEditPlace(p1);
		case WEBHOST:
			return new WebHostListPlace(p1);
		case WEBHOST_EDIT:
			return new WebHostEditPlace(p1);
		case LINK:
			return new LinkTreePlace(p1);
		case LINK_EDIT:
			return new LinkEditPlace(p1);
		case USER:
			return new UserListPlace(p1);
		case USER_EDIT:
			return new UserEditPlace(p1);
		case SITE_CONFIG:
			return new SiteConfigListPlace();
		case SITECONFIG_EDIT:
			return new SiteConfigEditPlace(p1);
		case GROUP:
			return new GroupTreePlace();
		case DISKFILE:
			return new DiskFileListPlace(p1);
		case DISKFILE_EDIT:
			return new DiskFileListPlace(p1);
		case GROUP_EDIT:
			return new GroupEditPlace(p1);
		case MENULEVEL:
			return new MenuLevelListPlace();
		case MENULEVEL_EDIT:
			return new MenuLevelEditPlace(p1);
		case XJ_CAT:
			return new XinJianCatTreePlace(p1);
		case XJ_CAT_EDIT:
			return new XinJianCatEditPlace(p1);
		case XINJIAN:
			return new XinJianListPlace(p1);
		case XINJIAN_EDIT:
			return new XinjianEditPlace(p1);
		case OBJECT_CLASS:
			return new ObjectClassListPlace();
		case OBJECT_CLASS_EDIT:
			return new ObjectClassEditPlace(p1);
		case PAGE_MISTAKE:
			return new PageMistakeListPlace(p1);
		case PAGE_MISTAKE_EDIT:
			return new PageMistakeEditPlace(p1);
		case ROLE:
			return new RolePlace();
		case ROLE_EDIT:
			return new RoleEditPlace(p1);
		case VOTE:
			return new VoteTreePlace(p1);
		case VOTE_EDIT:
			return new VoteEditPlace(p1);
		case VOTE_TPL:
			return new VoteTplListPlace();
		case VOTE_TPL_EDIT:
			return new VoteTplEditPlace(p1);
		case COMMENT:
			return new CommentTreePlace(p1);
		case COMMENT_EDIT:
			return new CommentEditPlace(p1);
		case FTL:
			return new FtlListPlace();
		case FTL_EDIT:
			return new FtlEditPlace(p1);
		case HMESSAGE_RECEIVE:
			return new HmessagePlace();
		case ASYNC_TASK:
			return new AsyncTaskListPlace();
		case URL_STATISTICS:
			return new UrlStatisticsListPlace(p1);
		case TABLE_SCHEMA:
			return new TableSchemaListPlace(p1);
		case TABLE_SCHEMA_EDIT:
			return new TableSchemaEditPlace(p1);
		case DATA_TEMPLATE:
			return new DataTemplateListPlace(p1);
		case DATA_TEMPLATE_EDIT:
			return new DataTemplateEditPlace(p1);
		default:
			return new WelcomePlace();
		}
	}
	
	private boolean fromHistory = false;
	
	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		fromHistory = true;
		gotoPlace(token2Place(event.getValue()));
	}
	
	public void handleCurrentHistory(){
		String h = History.getToken();
		gotoPlace(h);
	}
	
	public void gotoPlace(String token){
		gotoPlace(token2Place(token));
	}
	
	public void gotoPlace(MyPlace place){
		Iview view = null;
		if(place instanceof WelcomePlace){
			view = vs.getView(ViewNameEnum.WELCOME).initMe();
		}else if(place instanceof FieldEnumPlace){
			view = vs.getView(ViewNameEnum.FIELD_ENUM).initMe();
		}else if(place instanceof FieldEnumEditPlace){
			view = vs.getView(ViewNameEnum.FIELD_ENUM_EIDT).initMe();
		}else if(place instanceof MenuItemPlace){
			view = vs.getView(ViewNameEnum.MENUITEM).initMe();
		}else if(place instanceof MenuItemEditPlace){
			view  = vs.getView(ViewNameEnum.MENUITEM_EDIT).initMe();
		}else if(place instanceof WebSitePlace){
			view = vs.getView(ViewNameEnum.WEBSITE).initMe();
		}else if(place instanceof WebSiteEditPlace){
			view = vs.getView(ViewNameEnum.WEBSITE_EDIT).initMe();
		}else if(place instanceof ArticleListPlace){
			view = vs.getView(ViewNameEnum.ARTICLE).initMe();
		}else if(place instanceof SectionTreePlace){
			view = vs.getView(ViewNameEnum.SECTION).initMe();
		}else if(place instanceof SectionEditPlace){
			view = vs.getView(ViewNameEnum.SECTION_EDIT).initMe();
		}else if(place instanceof ArticleEditPlace){
			view = vs.getView(ViewNameEnum.ARTICLE_EDIT).initMe();
		}else if(place instanceof WebHostListPlace){
			view = vs.getView(ViewNameEnum.WEBHOST).initMe();
		}else if(place instanceof WebHostEditPlace){
			view = vs.getView(ViewNameEnum.WEBHOST_EDIT).initMe();
		}else if(place instanceof LinkTreePlace){
			view = vs.getView(ViewNameEnum.LINK).initMe();
		}else if(place instanceof LinkEditPlace){
			view = vs.getView(ViewNameEnum.LINK_EDIT).initMe();
		}else if(place instanceof UserListPlace){
			view = vs.getView(ViewNameEnum.USER).initMe();
		}else if(place instanceof UserEditPlace){
			view = vs.getView(ViewNameEnum.USER_EDIT).initMe();
		}else if(place instanceof SiteConfigListPlace){
			view = vs.getView(ViewNameEnum.SITE_CONFIG).initMe();
		}else if(place instanceof SiteConfigEditPlace){
			view = vs.getView(ViewNameEnum.SITECONFIG_EDIT).initMe();
		}else if(place instanceof GroupTreePlace){
			view = vs.getView(ViewNameEnum.GROUP).initMe();
		}else if(place instanceof DiskFileListPlace){
			view = vs.getView(ViewNameEnum.DISKFILE).initMe();
		}else if(place instanceof DiskFileEditPlace){
			view = vs.getView(ViewNameEnum.DISKFILE_EDIT).initMe();
		}else if(place instanceof GroupEditPlace){
			view = vs.getView(ViewNameEnum.GROUP_EDIT).initMe();
		}else if(place instanceof MenuLevelListPlace){
			view = vs.getView(ViewNameEnum.MENULEVEL).initMe();
		}else if(place instanceof MenuLevelEditPlace){
			view = vs.getView(ViewNameEnum.MENUITEM_EDIT).initMe();
		}else if(place instanceof AssetListPlace){
			view = vs.getView(ViewNameEnum.ASSET).initMe();
		}else if(place instanceof AssetEditPlace){
			view = vs.getView(ViewNameEnum.ASSET_EDIT).initMe();
		}else if(place instanceof AssetFolderTreePlace){
			view = vs.getView(ViewNameEnum.ASSET_FOLDER).initMe();
		}else if(place instanceof AssetFolderEditPlace){
			view = vs.getView(ViewNameEnum.ASSET_FOLDER_EDIT).initMe();
		}else if(place instanceof XinJianCatTreePlace){
			view = vs.getView(ViewNameEnum.XJ_CAT).initMe();
		}else if(place instanceof XinJianCatEditPlace){
			view = vs.getView(ViewNameEnum.XJ_CAT_EDIT).initMe();
		}else if(place instanceof XinJianListPlace){
			view = vs.getView(ViewNameEnum.XINJIAN).initMe();
		}else if(place instanceof XinjianEditPlace){
			view = vs.getView(ViewNameEnum.XINJIAN_EDIT).initMe();
		}else if(place instanceof ObjectClassListPlace){
			view = vs.getView(ViewNameEnum.OBJECT_CLASS).initMe();
		}else if(place instanceof ObjectClassEditPlace){
			view = vs.getView(ViewNameEnum.OBJECT_CLASS_EDIT).initMe();
		}else if(place instanceof PageMistakeListPlace){
			view = vs.getView(ViewNameEnum.PAGE_MISTAKE).initMe();
		}else if(place instanceof PageMistakeEditPlace){
			view = vs.getView(ViewNameEnum.PAGE_MISTAKE_EDIT).initMe();
		}else if(place instanceof RolePlace){
			view = vs.getView(ViewNameEnum.ROLE).initMe();
		}else if(place instanceof RoleEditPlace){
			view = vs.getView(ViewNameEnum.ROLE_EDIT).initMe();
		}else if(place instanceof VoteTreePlace){
			view = vs.getView(ViewNameEnum.VOTE).initMe();
		}else if(place instanceof VoteEditPlace){
			view = vs.getView(ViewNameEnum.VOTE_EDIT).initMe();
		}else if(place instanceof VoteTplListPlace){
			view = vs.getView(ViewNameEnum.VOTE_TPL).initMe();
		}else if(place instanceof VoteTplEditPlace){
			view = vs.getView(ViewNameEnum.VOTE_TPL_EDIT).initMe();
		}else if(place instanceof HmessagePlace){
			view = vs.getView(ViewNameEnum.HMESSAGE_RECEIVE).initMe();
		}else if(place instanceof AsyncTaskListPlace){
			view = vs.getView(ViewNameEnum.ASYNC_TASK).initMe();
		}else if(place instanceof UrlStatisticsListPlace){
			view = vs.getView(ViewNameEnum.URL_STATISTICS).initMe();
		}else if(place instanceof TableSchemaListPlace){
			view = vs.getView(ViewNameEnum.TABLE_SCHEMA).initMe();
		}else if(place instanceof TableSchemaEditPlace){
			view = vs.getView(ViewNameEnum.TABLE_SCHEMA_EDIT).initMe();
		}else if(place instanceof FtlListPlace){
			view = vs.getView(ViewNameEnum.FTL).initMe();
		}else if(place instanceof FtlEditPlace){
			view = vs.getView(ViewNameEnum.FTL_EDIT).initMe();
		}else if(place instanceof DataTemplateListPlace){
			view = vs.getView(ViewNameEnum.DATA_TEMPLATE).initMe();
		}else if(place instanceof DataTemplateEditPlace){
			view = vs.getView(ViewNameEnum.DATA_TEMPLATE_EDIT).initMe();
		}
		
		if(view != null){
			dp.setVisibleMember(view.asLayout());
			view.onDisplay(place);
		}
		if(!fromHistory){
			History.newItem(place.getToken(), false);
		}
		fromHistory = false;
	}
}
